<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题名称" prop="titleName">
        <el-input
          v-model="queryParams.titleName"
          placeholder="请输入标题名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="typeIds">
        <el-select v-model="queryParams.typeIds" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="type in goodsTypeList"
            :label="type.typeName" :value="type.typeId" />
        </el-select>
      </el-form-item>
      <el-form-item label="苹果专属" prop="isApple">
        <el-select v-model="queryParams.isApple" clearable size="small">
          <el-option label="否" :value="1" />
          <el-option label="是" :value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="分类" prop="topicType">
        <el-select v-model="queryParams.topicType" clearable size="small">
          <el-option v-for="it in topicTypeOptions"
                     :value="it.dictValue" :label="it.dictLabel" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:testTitle:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:testTitle:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:testTitle:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:testTitle:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="testTitleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题编号" align="center" prop="titleId" />
      <el-table-column label="标题名称" align="center" prop="titleName">
        <template slot-scope="scope">
          <router-link :to="'/test/values/' + scope.row.titleId" class="link-type">
            <span>{{scope.row.titleName}}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="titleSort" />
      <el-table-column label="类型" align="center" prop="typeIds" :formatter="goodsTypeFormatter" />
      <el-table-column label="苹果专属" align="center" prop="isApple">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isApple === 1" type="info">否</el-tag>
          <el-tag v-else>是</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="topicType">
        <template v-slot="scope">
          <el-tag v-for="it in topicTypeOptions"
                  v-if="scope.row.topicType === it.dictValue"
                  effect="plain" type="info">{{it.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:testTitle:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:testTitle:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改验机选项标题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题名称" prop="titleName">
          <el-input v-model="form.titleName" placeholder="请输入标题名称" />
        </el-form-item>
        <el-form-item label="排序" prop="titleSort">
          <el-input v-model="form.titleSort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="类型" prop="typeIds">
          <el-select v-model="form.typeIds" multiple placeholder="请选择类型">
            <el-option
              v-for="type in goodsTypeList"
              :label="type.typeName" :value="type.typeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="苹果专属" prop="isApple">
          <el-radio-group v-model="form.isApple">
            <el-radio :label="1">否</el-radio>
            <el-radio :label="2">是</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="分类" prop="topicType">
            <el-select v-model="form.topicType" placeholder="请选择分类">
              <el-option v-for="it in topicTypeOptions"
                         :label="it.dictLabel" :value="it.dictValue" />
            </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTestTitle, getTestTitle, delTestTitle, addTestTitle, updateTestTitle, exportTestTitle } from "@/api/basic/testTitle";
import {listType} from "@/api/share/type"
import {strTranArray, arrayTranStr} from "@/utils/tran/shareUtils"

export default {
  name: "TestTitle",
  components: {
  },
  data() {
    return {
      topicTypeOptions: [],
      // 商品类型列表
      goodsTypeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 验机选项标题表格数据
      testTitleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        titleName: null,
        titleSort: null,
        typeIds: null,
        isApple: null,
        topicType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        titleName: [
          { required: true, message: "标题名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getGoodsTypeList();
    this.getDicts("basic_test_title_topic_type").then(response => {
      this.topicTypeOptions = response.data;
    });
  },
  methods: {
    goodsTypeFormatter(row) {
      let res = "";
      let arr = row.typeIds.split(",");
      for(let i = 0; i < arr.length; i++) {
        for(let j = 0; j < this.goodsTypeList.length;j++) {
          if(parseInt(arr[i]) === this.goodsTypeList[j].typeId) {
            res += this.goodsTypeList[j].typeName + "-";
          }
        }
      }
      return res.substring(0, res.length - 1);
    },
    /** 查询商品类型列表 */
    getGoodsTypeList() {
      listType({}).then(resp => {
        this.goodsTypeList = resp.rows;
      })
    },
    /** 查询验机选项标题列表 */
    getList() {
      this.loading = true;
      listTestTitle(this.queryParams).then(response => {
        this.testTitleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        titleId: null,
        titleName: null,
        titleSort: null,
        typeIds: null,
        isApple: 1,
        remark: null,
        topicType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.titleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加验机选项标题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const titleId = row.titleId || this.ids
      getTestTitle(titleId).then(response => {
        this.form = response.data;
        this.form.typeIds = strTranArray(this.form.typeIds);
        this.open = true;
        this.title = "修改验机选项标题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.typeIds = arrayTranStr(this.form.typeIds);
          if (this.form.titleId != null) {
            updateTestTitle(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTestTitle(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const titleIds = row.titleId || this.ids;
      this.$confirm('是否确认删除验机选项标题编号为"' + titleIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTestTitle(titleIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有验机选项标题数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTestTitle(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
